/*
Copyright 2021 LuJun

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished 
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { warn, error, EventTarget } from 'cc';

const TAG = 'lcc/core/base.ts';

/**
 * @zh
 * 版本号
 */
export const VERSION = '1.0.0';

/**
 * @zh
 * 默认异步等待次数，-1 表示不限制
 */
export const DEFAULT_ASYNC_WAIT_TIMES = -1;

/**
 * @zh
 * 自动加载数据表<br/>
 * 如果为true，那么会在加载游戏包的时候加载所有数据表。
 */
export const LOADTABLE_AUTO = true;

/**
 * @zh
 * 默认定时器组
 */
export const DEFAULT_TIMER_GROUP = 'default';

/**
 * @zh
 * 定时器时间片，秒
 */
export const TIMER_TIMESLICE = 1;

/**
 * @zh
 * HTTP心跳，不大于0表示不使用
 */
export const HTTP_HEARTTICK = -1;

/**
 * @zh
 * HTTP协议路径
 */
export const HTTP_PROTOCOL_PATH = '/protocol';

/**
 * @zh
 * WS心跳
 */
export const WEBSOCKET_HEARTTICK = 10;

// ------------------------------ 调试

/**
 * @zh
 * 调试 - 核心
 */
export const DEBUG_CORE = true;

/**
 * @zh
 * 调试 - 存档模块
 */
export const DEBUG_ARCHIVE = true;

/**
 * @zh
 * 调试 - 音频模块
 */
export const DEBUG_AUDIO = true;

/**
 * @zh
 * 调试 - 配置模块
 */
export const DEBUG_CONFIG = true;

/**
 * @zh
 * 调试 - 数据表模块
 */
export const DEBUG_TABLE = true;

/**
 * @zh
 * 调试 - 语言模块
 */
export const DEBUG_LANGUAGE = true;

/**
 * @zh
 * 调试 - 网络模块
 */
export const DEBUG_NETWORK = true;

/**
 * @zh
 * 调试 - 定时器模块
 */
export const DEBUG_TIMER = true;

// ------------------------------ 索引

/**
 * @zh
 * 索引路径-UI界面
 */
export const INDEXPATH_UI = 'ui';

/**
 * @zh
 * 索引路径-数据表
 */
export const INDEXPATH_TABLE = 'table';

/**
 * @zh
 * 索引路径-字体
 */
export const INDEXPATH_FONT = 'font';

/**
 * @zh
 * 索引路径-语言
 */
export const INDEXPATH_LANGUAGE = 'language';

/**
 * @zh
 * 索引路径-预制体
 */
export const INDEXPATH_PREFAB = 'prefab';

// ------------------------------ 事件

/**
 * @zh
 * 全局事件对象
 */
export const event = new EventTarget();

/**
 * @zh
 * 事件 - 异步完成
 */
export const EVENT_ASYNC_COMPLETE = '__ASYNC_COMPLETE__';

/**
 * @zh
 * 事件 - 包加载中
 */
export const EVENT_PACK_LOADING = '__PACK_LOADING__';

/**
 * @zh
 * 事件 - 包加载完成
 */
export const EVENT_PACK_LOADED = '__PACK_LOADED__';

/**
 * @zh
 * 事件 - 索引加载
 */
export const EVENT_INDEXES_LOAD = '__INDEXES_LOAD__';

/**
 * @zh
 * 事件 - 场景转换
 */
export const EVENT_SWITCH_SCENE = '__SWITCH_SCENE__';

/**
 * @zh
 * 事件 - 存档加载
 */
export const EVENT_ARCHIVE_LOAD = '__ARCHIVE_LOAD__';

/**
 * @zh
 * 事件 - 存档加载
 */
export const EVENT_ARCHIVE_SAVE = '__ARCHIVE_SAVE__';

/**
 * @zh
 * 事件 - 存档创建
 */
export const EVENT_ARCHIVE_CREATE = '__ARCHIVE_CREATE__';

/**
 * @zh
 * 事件 - 语言切换
 */
export const EVENT_LANGUAGE_SWITCH = '__LANGUAGE_SWITCH__';

/**
 * @zh
 * 事件 - UI界面打开
 */
export const EVENT_UI_OPENED = '__UI_OPENED__';

/**
 * @zh
 * 事件 - UI界面关闭
 */
export const EVENT_UI_CLOSED = '__UI_CLOSED__';

/**
 * @zh
 * 事件 - 网络线打开
 */
export const EVENT_NETLINE_ONOPEN = '__NETLINE_ONOPEN__';

/**
 * @zh
 * 事件 - 网络线关闭
 */
export const EVENT_NETLINE_ONCLOSE = '__NETLINE_ONCLOSE__';

/**
 * @zh
 * 事件 - 网络线错误
 */
export const EVENT_NETLINE_ONERROR = '__NETLINE_ONERROR__';

/**
 * @zh
 * 事件 - 网络线分发数据
 */
export const EVENT_NETLINE_DISPATCH = '__NETLINE_DISPATCH__';

// ------------------------------ 冲突

/**
 * @zh
 * 冲突作为错误处理
 */
export const CONFLICT_AS_ERROR = true;

/**
 * @zh
 * 日志 - 冲突
 *
 * @param tag 标签
 * @param msg 消息
 *
 * @returns boolean 是否看作错误
 */
export function conflict(tag: string, msg: string) {
    if (CONFLICT_AS_ERROR) {
        error(tag, msg);
    } else {
        warn(tag, msg);
    }
    return CONFLICT_AS_ERROR;
}
